const { validLogin } = require('../utils/util');
function request(method, url, data){
  const valid =  validLogin(data.url);
  if(!valid){
    return 
  }
  return new Promise((resolve, reject) => {
    wx.request({
      method,
      url,
      data: {
        accessToken: wx.getStorageSync('accessToken'),
        appId: "a301020000000000ac22dc8fa20e3795",
        timestamp: Date.now(),
        ...data,
      },
      success({data}){
        // console.log(data)
        if(data.code === 200){
          resolve(data.data.responseData);
          return 
        }
        wx.showToast({
          icon: 'none',
          title: data.message,
          duration: 2000
        })
      }, 
      fail(e){
        console.log("错误信息: "+e)
      }
    })
  })
}
// 登录
function login(data){
  return request('post', 'https://interface.music.163.com/api/growth/external/sandbox/request', {
    bizContentJson: JSON.stringify({
      clientId: data.clientId
    }),
    device: "{\"deviceType\":\"andrwear\",\"os\":\"andrwear\",\"appVer\":\"0.1\",\"channel\":\"hm\",\"model\":\"kys\",\"deviceId\":\"321\",\"brand\":\"hm\",\"osVer\":\"8.1.0\"}",
    url: "/openapi/music/basic/oauth2/login/anonymous"
  })
}


function gethottext(data){
  return request("post","https://interface.music.163.com/api/growth/external/sandbox/request",{
    bizContentJson:JSON.stringify(data),
    url:"/openapi/music/basic/search/hot/keyword/get/v2",
    device: "{\"deviceType\":\"andrwear\",\"os\":\"andrwear\",\"appVer\":\"0.1\",\"channel\":\"hm\",\"model\":\"kys\",\"deviceId\":\"321\",\"brand\":\"hm\",\"osVer\":\"8.1.0\"}",
    // accessToken:data.accessToken,
    // appId:data.appId,
    // timestamp:data.timestamp
  })
}

function recommodsong(data){
  return request("post","https://interface.music.163.com/api/growth/external/sandbox/request",{
    bizContentJson:JSON.stringify(data),
    device: "{\"deviceType\":\"andrwear\",\"os\":\"andrwear\",\"appVer\":\"0.1\",\"channel\":\"hm\",\"model\":\"kys\",\"deviceId\":\"321\",\"brand\":\"hm\",\"osVer\":\"8.1.0\"}",
    url: "/openapi/music/basic/recommend/songlist/get/v2"
  })
}
// function dailyrecommod(data){
//   return request("post","https://interface.music.163.com/api/growth/external/sandbox/request",{
//     bizContentJson:"{\"limit\":\"5\"}",
//     url:"/openapi/music/basic/recommend/songlist/get/v2",
//     device: "{\"deviceType\":\"andrwear\",\"os\":\"andrwear\",\"appVer\":\"0.1\",\"channel\":\"hm\",\"model\":\"kys\",\"deviceId\":\"321\",\"brand\":\"hm\",\"osVer\":\"8.1.0\"}"
//   })
// }



/**
 * 获取歌曲播放URL
 * @param {} data 
 */
function getSongPlayURL(songId){
  return request("post","https://interface.music.163.com/api/growth/external/sandbox/request",{
    bizContentJson:JSON.stringify({
      songId: songId
    }),
    device: "{\"deviceType\":\"andrwear\",\"os\":\"andrwear\",\"appVer\":\"0.1\",\"channel\":\"hm\",\"model\":\"kys\",\"deviceId\":\"321\",\"brand\":\"hm\",\"osVer\":\"8.1.0\"}",
    url:"/openapi/music/basic/song/playurl/get/v2"
  })
}

/**
 * 获取歌曲信息
 * @param {} data 
 */
function getSongInformation(songId){
  return request("post","https://interface.music.163.com/api/growth/external/sandbox/request",{
    bizContentJson:JSON.stringify({
      songId: songId,
      withUrl:"true",
    }),
    device: "{\"deviceType\":\"andrwear\",\"os\":\"andrwear\",\"appVer\":\"0.1\",\"channel\":\"hm\",\"model\":\"kys\",\"deviceId\":\"321\",\"brand\":\"hm\",\"osVer\":\"8.1.0\"}",
    url:"/openapi/music/basic/song/detail/get/v2"
  })
}

/**
 * 获取歌曲评论
 * @param {*} data 
 */
function getSongCommonts(songId){
  return request("post","https://interface.music.163.com/api/growth/external/sandbox/request",{
    bizContentJson:JSON.stringify({
      resourceId: songId,
      type:"song",
      limit:"20",
      offset:"0"
    }),
    device: "{\"deviceType\":\"andrwear\",\"os\":\"andrwear\",\"appVer\":\"0.1\",\"channel\":\"hm\",\"model\":\"kys\",\"deviceId\":\"321\",\"brand\":\"hm\",\"osVer\":\"8.1.0\"}",
    url:"/openapi/music/basic/comments/get/v2"
  })
}

/**
 * 获取歌词及其时间
 * @param {*} songId 
 */
function getSongLyric(songId){
  return request("post","https://interface.music.163.com/api/growth/external/sandbox/request",{
    bizContentJson:JSON.stringify({
      songId: songId,
    }),
    device: "{\"deviceType\":\"andrwear\",\"os\":\"andrwear\",\"appVer\":\"0.1\",\"channel\":\"hm\",\"model\":\"kys\",\"deviceId\":\"321\",\"brand\":\"hm\",\"osVer\":\"8.1.0\"}",
    url:"/openapi/music/basic/song/lyric/get/v2"
  })
}

/**
 * 获取收藏歌单
 * @param {*} data 
 */
function getUserCollectionSubs(data){
  return request('post', 'https://interface.music.163.com/api/growth/external/sandbox/request', {
    bizContentJson: JSON.stringify(data),
    url: "/openapi/music/basic/playlist/subed/get/v2"
  })
}
/**
 * 获取自建歌单
 * @param {*} data 
 */
function getUserBulidCollections(data){
  return request('post', 'https://interface.music.163.com/api/growth/external/sandbox/request', {
    bizContentJson: JSON.stringify(data),
    url: "/openapi/music/basic/playlist/created/get/v2"
  }) 
}
/**
 * 红心歌单添加或删除歌曲
 * @param {*} data 
 */
function addOrDelInLoveAlbum(data){
  return request('post', 'https://interface.music.163.com/api/growth/external/sandbox/request', {
    bizContentJson: JSON.stringify(data),
    url: "/openapi/music/basic/playlist/song/like/v2"
  }) 
}

/**
 * 收藏歌单
 * @param {*} data 
 */
function doUserCollectionSubs(data){
  return request('post', 'https://interface.music.163.com/api/growth/external/sandbox/request', {
    bizContentJson: JSON.stringify(data),
    url: "/openapi/music/basic/playlist/sub/v2"
  })
  
}
/**
 * 取消收藏歌单
 * @param {*} data 
 */
function cancelUserCollectionSubs(data){
  return request(
    'post', 
    'https://interface.music.163.com/api/growth/external/sandbox/request', 
    {
      bizContentJson: JSON.stringify(data),
      url: "/openapi/music/basic/playlist/unsub/v2",
      
    })
    
}
/**
 * 点击歌单进入详情页面
 * @param {*} data 
 */
function getUserCollectionDetalis(data){
  return request(
    'post', 
    'https://interface.music.163.com/api/growth/external/sandbox/request', {
    bizContentJson: JSON.stringify(data),
    url: "/openapi/music/basic/playlist/song/list/get/v2",
  })
}
/**推荐歌单 */
function getRecommodList(data){
  return request("post","https://interface.music.163.com/api/growth/external/sandbox/request",{
    "url": "/openapi/music/basic/recommend/playlist/get",
    device: "{\"deviceType\":\"andrwear\",\"os\":\"andrwear\",\"appVer\":\"0.1\",\"channel\":\"hm\",\"model\":\"kys\",\"deviceId\":\"321\",\"brand\":\"hm\",\"osVer\":\"8.1.0\"}",
    bizContentJson: "{\"limit\":\"20\",\"offset\":\"0\"}"
  })
}

/**
 * 根据关键字搜索歌曲
 * @param {*} data 
 */
function searchMusicsByKeywords(data){
  return request(
    'post', 
    'https://interface.music.163.com/api/growth/external/sandbox/request', 
    {
      bizContentJson: JSON.stringify(data),
      device: "{\"deviceType\":\"andrwear\",\"os\":\"andrwear\",\"appVer\":\"0.1\",\"channel\":\"hm\",\"model\":\"kys\",\"deviceId\":\"321\",\"brand\":\"hm\",\"osVer\":\"8.1.0\"}",
      url: "/openapi/music/basic/search/song/get/v2",
    })
}

/**
 * 根据关键字搜索歌单
 * @param {*} data 
 */
function searchMusicListByKeywords(data){
  return request(
    'post', 
    'https://interface.music.163.com/api/growth/external/sandbox/request', 
    {
      bizContentJson: JSON.stringify(data),
      device: "{\"deviceType\":\"andrwear\",\"os\":\"andrwear\",\"appVer\":\"0.1\",\"channel\":\"hm\",\"model\":\"kys\",\"deviceId\":\"321\",\"brand\":\"hm\",\"osVer\":\"8.1.0\"}",
      url: "/openapi/music/basic/search/playlist/get/v2",
    })
}

/**
 * 根据关键字搜索专辑
 * @param {*} data 
 */
function searchMusicsAlbumsByKeywords(data){
  return request(
    'post', 
    'https://interface.music.163.com/api/growth/external/sandbox/request', 
    {
      bizContentJson: JSON.stringify(data),
      device: "{\"deviceType\":\"andrwear\",\"os\":\"andrwear\",\"appVer\":\"0.1\",\"channel\":\"hm\",\"model\":\"kys\",\"deviceId\":\"321\",\"brand\":\"hm\",\"osVer\":\"8.1.0\"}",
      url: "/openapi/music/basic/search/album/get/v2",
    })
}

module.exports = {
  login, getUserCollectionSubs,gethottext,recommodsong,cancelUserCollectionSubs,getUserCollectionDetalis,doUserCollectionSubs,getSongPlayURL,getSongInformation,getSongCommonts,getSongLyric,getRecommodList,searchMusicsByKeywords,searchMusicListByKeywords,searchMusicsAlbumsByKeywords,getUserBulidCollections,addOrDelInLoveAlbum
}